package com.dodoke.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dodoke.dto.RetailServiceCenterDTO;
import com.dodoke.entity.RetailServiceCenter;
import com.dodoke.entity.RetailWarehouse;
import com.dodoke.mapper.RetailServiceCenterMapper;
import com.dodoke.service.IRetailServiceCenterService;
import com.dodoke.vo.RetailServiceCenterAllListVO;
import com.dodoke.vo.RetailServiceCenterDetailVO;
import com.dodoke.vo.RetailServiceCenterListVO;
import com.dodoke.vo.RetailWarehouseListVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务中心(奶站)业务实现
 */
@Service
public class RetailServiceCenterServiceImpl extends ServiceImpl<RetailServiceCenterMapper, RetailServiceCenter> implements IRetailServiceCenterService {
    @Resource
    private RetailServiceCenterMapper retailServiceCenterMapper;

    /**
     * 服务中心(奶站)分页查询
     *
     * @param currentPage            当前页码
     * @param pageSize               每页显示条数
     * @param retailServiceCenterDTO 模糊查询接收参数
     * @return
     */
    @Override
    public Page<RetailServiceCenterListVO> getListByPage(Integer currentPage, Integer pageSize, RetailServiceCenterDTO retailServiceCenterDTO) {
        Page<RetailServiceCenter> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<RetailServiceCenter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .select(RetailServiceCenter::getId,
                        RetailServiceCenter::getName,
                        RetailServiceCenter::getCode,
                        RetailServiceCenter::getAddress,
                        RetailServiceCenter::getTel,
                        RetailServiceCenter::getSupplyCode,
                        RetailServiceCenter::getType,
                        RetailServiceCenter::getLevel,
                        RetailServiceCenter::getStatus)
                .orderByAsc(RetailServiceCenter::getSortNo)
                .eq(!StringUtils.isEmpty(retailServiceCenterDTO.getStatus()), RetailServiceCenter::getStatus, retailServiceCenterDTO.getStatus())
                .and(!StringUtils.isEmpty(retailServiceCenterDTO.getNameAndCode()), res -> {
                    res.like(RetailServiceCenter::getName, retailServiceCenterDTO.getNameAndCode());
                    res.or();
                    res.like(RetailServiceCenter::getCode, retailServiceCenterDTO.getNameAndCode());
                });
        page = retailServiceCenterMapper.selectPage(page, queryWrapper);
        List<RetailServiceCenterListVO> recordsVO = page.getRecords().stream()
                .map(e -> JSON.parseObject(JSON.toJSONString(e), RetailServiceCenterListVO.class))
                .collect(Collectors.toList());
        Page<RetailServiceCenterListVO> resPage = new Page<>(currentPage, pageSize);
        resPage.setRecords(recordsVO);
        resPage.setTotal(page.getTotal());
        resPage.setPages(page.getPages());
        ;
        return resPage;
    }

    /**
     * 服务中心新增
     *
     * @param retailServiceCenter
     * @return
     */
    @Override
    public int saveRetailServiceCenter(RetailServiceCenter retailServiceCenter) {
        retailServiceCenter.setDelFlag("0");
        return retailServiceCenterMapper.insertRetailServiceCenter(retailServiceCenter);
    }

    /**
     * 服务中心(奶站)修改
     *
     * @param retailServiceCenter
     * @return
     */
    @Override
    public int editRetailServiceCenter(RetailServiceCenter retailServiceCenter) {
        return retailServiceCenterMapper.updateRetailServiceCenter(retailServiceCenter);
    }

    /**
     * 根据id获取服务中心(奶站)信息
     *
     * @param id
     * @return
     */
    @Override
    public RetailServiceCenterDetailVO getById(Long id) {
        RetailServiceCenter serviceCenter = retailServiceCenterMapper.selectById(id);
        RetailServiceCenterDetailVO retailServiceCenterDetailVO = JSON.parseObject(JSON.toJSONString(serviceCenter), RetailServiceCenterDetailVO.class);
        return retailServiceCenterDetailVO;
    }

    /**
     * 获取所有奶站名称
     *
     * @return
     */
    @Override
    public List<RetailServiceCenterAllListVO> getAllList() {
        LambdaQueryWrapper<RetailServiceCenter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(
                RetailServiceCenter::getId,
                RetailServiceCenter::getName).orderByAsc(RetailServiceCenter::getSortNo);
        List<RetailServiceCenter> retailServiceCenterList = retailServiceCenterMapper.selectList(queryWrapper);
        List<RetailServiceCenterAllListVO> records = retailServiceCenterList.stream()
                .map(e -> JSON.parseObject(JSON.toJSONString(e), RetailServiceCenterAllListVO.class))
                .collect(Collectors.toList());
        return records;
    }
}
